package com.px.user.mapper;

import com.px.user.entity.Course;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface CourseMapper {

    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into course (creatorId,teacherId,roomIds,roomName,roomIntro,pushSource,fetchSource,startTime,endTime) " +
            "values (#{creatorId},#{teacherId},#{roomIds},#{roomName},#{roomIntro},#{pushSource},#{fetchSource},#{startTime},#{endTime})")
    void insert(Course course);

    @Select("select * from course where json_search(roomIds, 'one', #{qroomId}) is not null and startTime<#{qend} " +
            "and endTime>current_timestamp order by startTime asc")
    List<Course> findByRoomId(Course course);

    @Select("update course set roomIds=ifnull(#{roomIds},roomIds)," +
            "roomIntro=ifnull(#{roomIntro},roomIntro),pushSource=ifnull(#{pushSource},pushSource)," +
            "fetchSource=ifnull(#{fetchSource},fetchSource),startTime=ifnull(#{startTime},startTime)," +
            "endTime=ifnull(#{endTime},endTime),teacherId=ifnull(#{teacherId},teacherId) " +
            "where creatorId=#{creatorId} and id=#{id}")
    void update(Course course);

    @Delete("delete from course where id=#{id}")
    void delete(Integer id);

    @Delete("delete from course where id=#{id} and creatorId=#{creatorId}")
    void deleteByCreatorId(Map map);

    @Select("select id from course where teacherId=#{id} and " +
            "current_timestamp between date_add(startTime, interval -5 minute) " +
            "and date_add(endTime, interval 1 minute) limit 1")
    Integer pushCheck(Long id);

    @Select("select id from course where json_search(roomIds, 'one', #{qroomId}) is not null and " +
            "(#{startTime} between startTime and endTime or #{endTime} between startTime and endTime) limit 1")
    Integer roomCourseCheck(Course course);

    @Select("select id from course where id <> #{id} and json_search(roomIds, 'one', #{qroomId}) is not null and " +
            "(#{startTime} between startTime and endTime or #{endTime} between startTime and endTime) limit 1")
    Integer roomCourseCheckExcludeMe(Course course);

    @Select({"<script>",
            "select course.*,user.nickname from course left join user on course.teacherId=user.id",
            "<where>",
            "<if test='qstart != null'>and course.startTime >= #{qstart}</if>",
            "<if test='qend != null'>and course.startTime <![CDATA[<=]]> #{qend}</if>",
            "<if test='id != null'>and course.id = #{id}</if>",
            "<if test='teacherId != null'>and course.teacherId = #{teacherId}</if>",
            "<if test='qroomId != null'>and json_search(course.roomIds, 'one', #{qroomId}) is not null</if>",
            "<if test='roomName != null'>and course.roomName like #{roomName}</if>",
            "</where>",
            "order by ${orderField} desc limit #{startIndex},#{limit}",
            "</script>"})
    @Results({@Result(property = "teacherNickname", column = "nickname")})
    List<Course> paging(Course course);

    @Select({"<script>",
            "select count(1) from course",
            "<where>",
            "<if test='qstart != null'>and startTime >= #{qstart}</if>",
            "<if test='qend != null'>and startTime <![CDATA[<=]]> #{qend}</if>",
            "<if test='id != null'>and id = #{id}</if>",
            "<if test='teacherId != null'>and teacherId = #{teacherId}</if>",
            "<if test='qroomId != null'>and json_search(roomIds, 'one', #{qroomId}) is not null</if>",
            "<if test='roomName != null'>and roomName like #{roomName}</if>",
            "</where>",
            "</script>"})
    Integer pagingCount(Course course);

    /**
     * 发送礼物检查
     * @param map creatorId,roomId
     * @return
     */
    @Select("select id from course where teacherId=#{teacherId} and json_search(roomIds, 'one', #{roomId}) is not null " +
            "and current_timestamp between startTime and endTime limit 1")
    Integer currentTeacherCheck(Map map);


    @Select("select * from course where teacherId=#{teacherId} and current_timestamp>startTime " +
            "and startTime<#{qend} and endTime>current_timestamp order by startTime asc")
    List<Course> findByTeacherId(Course course);
}
